package com.isoftstone.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.isoftstone.mapper.PaymentMapper;
import com.isoftstone.pojo.Payment;
import com.isoftstone.service.PaymentService;
import com.isoftstone.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;

@Service
public class PaymentServiceImpl implements PaymentService {
    private final PaymentMapper paymentMapper;

    @Autowired
    public PaymentServiceImpl(PaymentMapper paymentMapper) {
        this.paymentMapper = paymentMapper;
    }

    @Override
    public Result<?> insertPayment(Payment payment) {
        int result = paymentMapper.insert(payment);
        if (result > 0){
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @Override
    public Result<?> deletePayment(Integer id) {
        int result = paymentMapper.deleteById(id);
        if (result > 0){
            return Result.success("删除成功");
        }
        return Result. error("删除失败");
    }

    @Override
    @Transactional
    public Result<?> updatePayment(Integer id, Payment payment) {
        int result = paymentMapper.update(payment);
        if (result > 0){
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }

    @Override
    public Optional<Payment> getPaymentById(Integer id) {
        return Optional.ofNullable(paymentMapper.selectById(id));
    }

    @Override
    public Page getAllPayments(Integer currentPage, Integer pageSize) {
        Page<Payment> page = PageHelper.startPage(currentPage, pageSize);
        paymentMapper.selectAll();
        return page;
    }
    @Override
    public Page selectPaymentsBySemester(String semester,
                                                  Integer currentPage, Integer pageSize) {
        Page<Payment> page = PageHelper.startPage(currentPage, pageSize);
        paymentMapper.selectBySemester(semester);
        return page;
    }
    @Override
    public Page selectPaymentsByAcademicYear(String academicYear, Integer currentPage, Integer pageSize) {
        Page<Payment> page = PageHelper.startPage(currentPage, pageSize);
        paymentMapper.selectByAcademicYear(academicYear);
        return page;
    }

}
